import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    def __init__(self,ai_game):
        super().__init__()

        self.setting = ai_game.setting

        self.screen = ai_game.screen
        self.color = self.setting.bullet_color

        # 创建一个表示子弹的矩形，在飞船的顶部中央
        self.rect = pygame.Rect(0, 0, self.setting.bullet_width, self.setting.bullet_height)
        self.rect.midtop = ai_game.ship.rect.midtop

        # 存储子弹的位置
        self.y = float(self.rect.y)
    
    # 更新子弹位置
    def update(self):
        self.y -= self.setting.bullet_speed * self.setting.bullet_speed_rate
        self.rect.y = self.y

    # 画子弹
    def draw_bullet(self):
        pygame.draw.rect(self.screen, self.color, self.rect)